<template>
    <div style="height: 100%;width: 100%;">
        <iframe :src="site" id="iframe">
        </iframe>
    </div>
</template>
<script>
    import Vue from 'vue';
    import Crypt from '@/aes.js';
    import 'vant/lib/index.css'
    require('../../../static/css/product/info/style.css');
    import { Toast} from 'vant';
    Vue.use(Toast);
    export default {
        data() {
            return {
                parameters: '',
                site: '',
                skipSite: '',
                title: '',
                id: 0,
            }
        },
        mounted() {
            var _this = this,
            parameters = _this.$route.query.parameters ? _this.$route.query.parameters : '';
            if (parameters == '') {
                Toast.fail('参数异常');
                window.history.back();
                return false;
            }
            _this.parameters = JSON.parse(Crypt.dec(parameters));

            if(_this.parameters.title == '' || _this.parameters.url == '' || _this.parameters.user_id == ''){
                Toast.fail('参数异常');
                window.history.back();
                return false;
            }

            _this.site = _this.parameters.url;

            const iframe = document.querySelector('#iframe')
            // 处理兼容行问题
            if (iframe.attachEvent) {
                iframe.attachEvent('onload', function () {
                    _this.pageLoadingAccomplish(_this.parameters.type,_this.parameters.id,_this.parameters.deviceType)
                })
            } else {
                iframe.onload = function () {
                    _this.pageLoadingAccomplish(_this.parameters.type,_this.parameters.id,_this.parameters.deviceType)
                }
            }
        },
        methods: {
            pageLoadingAccomplish(type,id,deviceType){
                var _this = this;
                var userId = localStorage.getItem('userId');
                _this.$axios.post('/click?platform=3', {
                    t: _this.$get_time(),
                    type: type,
                    id: id,
                    deviceType: deviceType,
                    user_id: userId
                }).then((res) => {
                    if(res.code == 200){
                        _this.$axios.post('/duration?platform=3', {
                            t: _this.$get_time(),
                            app_version: '1.0',
                            type: type,
                            id: id,
                            user_id: userId
                        })
                        .then((res) => {
                            _this.addDuration(res.data)
                        })
                    }
                })
            },
            addDuration(id) {
                var _this = this;
                setInterval(function() {
                    var userId = localStorage.getItem('userId');
                    _this.$axios.post('/addDuration?platform=3', {
                        main_channel: 1,
                        t: _this.$get_time(),
                        app_version: '1.0',
                        id: id,
                        user_id: userId
                    }).then((res) => {

                    })
                }, 1000);
            },
            onClickLeft() {
                var type = this.$route.query.type ? this.$route.query.type : '';

                if (type == 1) {
                  this.skipSite = '/#/';
                } else if (type == 2) {
                  this.skipSite = '/#/list';
                }
            }
        }   
    }

</script>
